import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:t3_data_service/biz_data_source/map/address/poi_search/poi_search.dart';
import 'package:t3_data_service/data_provider/map_sctx/t3_map_sctx_provider.dart';
import 'package:t3_data_service/data_provider/orion/orion_hash.dart';
import 'package:t3_data_service/data_provider/orion/orion_service.dart';
import 'package:t3_map/t3_map_plugin/map/core/t3_location.dart';
import 'package:t3_map/t3_map_plugin/poi/poi_manager/t3_poi_data_service.dart';
import 'package:t3_map/t3_map_plugin/t3_map_platform_interface.dart';

class T3POIManager {
  /// 获取逆地理信息
  static Future<POIModel?> getRegeoFromService({required T3MapType mapType, required T3LocationCoordinate2D coordinate}) async {
    ///是否使用T3的API进行逆地理
    int mapApiBucket = OrionService.instance.switchInfo.mapApiBucket ?? 0;
    bool useApi = false;
    if (mapApiBucket > 0) {
      useApi = await OrionHashTool.bucketWithDeviceUUID(mapApiBucket);
    }
    if (useApi) {
      return T3POIDataService.getRegeoFromService(coordinate: coordinate);
    } 
    else {
      final routeValue = await T3MapPlatform.instance.getRegeo(mapType: mapType, coordinate: coordinate);
      if (routeValue != null) {
        try {
          final poi = POIModel(location: LatLng(coordinate.latitude, coordinate.longitude));
          final json = jsonDecode(routeValue);

          final error = json['error'];

          if (error is String && error.isNotEmpty) {
            debugPrint('error: $error');
            return null;
          }
          else {
            if (json["city"] is String) {
              poi.city = json["city"];
            }
            if (json['cityCode'] is String) {
              poi.cityCode = json['cityCode'];
            }
            if (json['adCode'] is String) {
              poi.adCode = json['adCode'];
            }
            if (json['district'] is String) {
              poi.district = json['district'];
            }
            final address = json['address'];
            if (address is String) {
              poi.address = address;
            }

            final shortAddress = json['shortAddress'];
            if (shortAddress is String) {
              /// 处理敏感词
              if (shortAddress.contains('公共厕所')) {
                poi.name = shortAddress.replaceAll('公共厕所', '公共设施');
              }
              else {
                poi.name = shortAddress;
              }
            }
            /// 如果没有短地址，则将长地址处理为短地址
            else {
                ///将地址使用区切割
                if (poi.district != null && address is String) {
                  final list = address.split(poi.district!);
                  if (list.isNotEmpty) {
                    poi.name = poi.district! + list.last;
                  }
                  else {
                    poi.name = poi.address;
                  }
                } else {
                  poi.name = poi.address;
                }
            }
            poi.mapType = mapType;
            return poi;
          }
        } catch (e) {
          return null;
        }
      }
      else {
        return null;
      }
    }
  }
}